<?php
 require('features-data.php');
 if (isset($_GET['action'])) {
  if ($_GET['action'] == 'edit' && isset($_GET['group']) 
      && isset($_GET['feature']) && isset($_GET['client']) 
      && isset($_GET['value'])) {
    setSupported($_GET['group'], $_GET['feature'], $_GET['client'], $_GET['value']);
    save();
  }

  if ($_GET['action'] == 'addClient' && isset($_GET['client'])) {
   addClient($_GET['client']);
   sortClients();
   save();
  }
  if ($_GET['action'] == 'sortClients') {
   sortClients();
   save();
  }
  if ($_GET['action'] == 'sortFeatures') {
   sortFeatures();
   save();
  }
  if ($_GET['action'] == 'addFeature' && isset($_GET['groupName']) && isset($_GET['featureName'])) {
   addFeature($_GET['groupName'], $_GET['featureName']);
   save();
  }
  if ($_GET['action'] == 'delClient' && isset($_GET['client'])) {
   delClient($_GET['client']);
   save();
  }
  if ($_GET['action'] == 'delFeatureGroup' && isset($_GET['groupName'])) {
   delFeatureGroup($_GET['groupName']);
   save();
  }
  if ($_GET['action'] == 'delFeature' && isset($_GET['featureName']) && isset($_GET['groupName'])) {
   delFeature($_GET['groupName'], $_GET['featureName']);
   save();
  }
 }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <title>DMDirc</title>
  <link rel="stylesheet" type="text/css" href="res/style.css">
    <style type="text/css">
   td, th, table { border: 1px solid #fff; border-collapse: collapse; padding: 5px; }
   td, tr#t th {
    text-align: center;
    width: 100px;
    overflow: hidden;
   }
   th { width: 150px; text-align: right; }
   td.y { background-color: green; }
   td.n { background-color: red; }
   td.m { background-color: orange; }
  </style>
  <script type="text/javascript">
   function setNewValue(group, feature, client, oldValue) {
     var newValue = prompt("Please enter new value (0=no,1=yes, 2=?, anything else is shown as is)", oldValue);
     window.location = "?action=edit&group="+group+"&feature="+feature+"&client="+client+"&value="+newValue;
   }
  </script>
 </head>
 <body>
  <h1>Feature Matrix - Management</h1>
  <p style="text-align: center">Manual reordering of feature groups and/or features (nasty)</p>
  <p style="text-align: center">Renaming of feature groups, features and clients</p>
  <p style="text-align: center"><a href="javascript:void(0)" 
        onClick="var clientName = prompt('Please enter the new clients name');
                 window.location='?action=addClient&client='+clientName">Add new Client</a></p>
  <p style="text-align: center"><a href="?action=sortClients">Sort Clients</a></p>
  <p style="text-align: center"><a href="?action=sortFeatures">Sort Features</a></p>
  <p style="text-align: center"><a href="javascript:void(0)" 
        onClick="var groupName = prompt('Please enter the (new) feature groups name');
                 var featureName = prompt('Please enter the new features name');
                 window.location='?action=addFeature&groupName='+groupName+'&featureName='+featureName">Add new feature</a></p>
  <table>
    <tr id="t">
     <td></td>
     <td></td>
   <?php
    foreach($clients as $clientName) {
     echo '<th><a href="javascript:void(0)" 
       onClick="if (confirm(\'Delete client?\')) {window.location=\'?action=delClient&client='.$clientName.'\'}">
       '.$clientName.'</a></th>'."\n";
    }
    echo '</tr>'."\n";
    foreach($features as $featureGroup => $featureGroupValue) {
    echo '<tr>'."\n".'<th rowspan="'.(count($featureGroupValue)+1).'"><a href="javascript:void(0)" 
       onClick="if (confirm(\'Delete feature group?\')){
          window.location=\'?action=delFeatureGroup&groupName='.$featureGroup.'\'}">'.$featureGroup.'</a><th>'."\n";
    echo '<td colspan="'.count($clients).'"></td>'."\n";
    echo '</tr>'."\n";
     foreach($featureGroupValue as $featureName => $featureSupportedBy) {
      echo '<tr>'."\n";
      echo '<th><a href="javascript:void(0)" 
       onClick="if (confirm(\'Delete feature?\')){
          window.location=\'?action=delFeature&groupName='.$featureGroup.'&featureName='.$featureName.'\'}">'.$featureName.'</a></th>'."\n";
       foreach($featureSupportedBy as $clientKey => $featureSupported) {
         if ($featureSupported == '0') {
           echo '<td class="n"><a href="javascript:void(0)" onClick="
                 setNewValue(\''.$featureGroup.'\', \''.$featureName.'\', 
                 \''.$clientKey.'\', 
                 \''.$featureSupported.'\')
           ">No</a></td>'."\n";
         } else if ($featureSupported == '1') {
           echo '<td class="y"><a href="javascript:void(0)" onClick="
                 setNewValue(\''.$featureGroup.'\', \''.$featureName.'\', 
                 \''.$clientKey.'\', 
                 \''.$featureSupported.'\')
           ">Yes</a></td>'."\n";
         } else if ($featureSupported == '2') {
           echo '<td class="m"><a href="javascript:void(0)" onClick="
                 setNewValue(\''.$featureGroup.'\', \''.$featureName.'\', 
                 \''.$clientKey.'\', 
                 \''.$featureSupported.'\')
           ">?</a></td>'."\n";
         }else {
           echo '<td class="m"><a href="javascript:void(0)" onClick="
                 setNewValue(\''.$featureGroup.'\', \''.$featureName.'\', 
                 \''.$clientKey.'\', 
                 \''.$featureSupported.'\')
           ">'.$featureSupported.'</a></td>'."\n";
         }
       }
      echo '</tr>'."\n";
     }
     echo '<tr>'."\n".'<td></td>'."\n".'</tr>'."\n";
    }
   ?>
   </table>
 </body>
</html>